package com.easygem.front.service.imp;

import com.easygem.common.error.SystemCodeEnum;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.front.ClientType;
import com.easygem.common.vo.front.ClientTypeVo;
import com.easygem.front.converter.ClientTypeConverter;
import com.easygem.front.mapper.FrontClientTypeMapper;
import com.easygem.front.service.FrontClientTypeService;
import com.easygem.front.service.base.AbstractServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author 何维佳
 * @version 1.0
 * @className ClienttypeServiceImpl
 * @Description TODO
 * @date 2024/12/26 19:43
 */
@Service("frontClientTypeService")
public class FrontClientTypeServiceImpl extends AbstractServiceImpl<FrontClientTypeMapper, ClientType,Integer> implements FrontClientTypeService {

    @Autowired
    private FrontClientTypeMapper frontClientTypeMapper;

    @Override
    public List<ClientType> findClienttypeList(String accountName) {
        //获得到登录后的用户信息，通用方式
        return frontClientTypeMapper.selectClienttype(accountName);
    }

    /**
     * 添加客户类型
     *
     * @param clientTypeVo 客户类型对象
     */
    @Override
    public void add(ClientTypeVo clientTypeVo) {
        clientTypeVo.setCreateTime(new Timestamp(System.currentTimeMillis()));
        ClientType clientType = new ClientType();
        //获得到登录后的用户信息，通用方式
        BeanUtils.copyProperties(clientTypeVo, clientType);
        frontClientTypeMapper.insert(clientType);
    }

    /**
     * 得到所有客户类型的所有信息
     *
     * @return 客户类型集合列表
     */
    @Override
    public List<ClientTypeVo> getClientTypeAll(String accountName) {
        return ClientTypeConverter.converterToClientTypeVoList(frontClientTypeMapper.getClientTypeList(accountName));
    }

    /**
     * 查询所有已启用的
     *
     * @return 用于下拉列表框
     */
    @Override
    public List<ClientTypeVo> getClientTypeListByEnable(String accountName) {
        //return ClientTypeConverter.converterToClientTypeVoList(frontClientTypeMapper.getClientTypeListByEnable(accountName));
        return ClientTypeConverter.converterToClientTypeVoList(frontClientTypeMapper.selectClienttype(accountName));
    }

    /**
     * 拿到需要编辑客户类型的信息
     *
     * @param id id
     * @return
     */
    @Override
    public ClientTypeVo edit(Integer id) throws SystemException {
        ClientType clientType = frontClientTypeMapper.selectByPrimaryKey(id);
        if(clientType==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的客户状态不存在");
        }
        return ClientTypeConverter.converterToClientTypeVo(clientType);
    }
    /**
     * 更新客户类型
     *
     * @param clientTypeVo 客户类型对象
     */
    @Override
    public void update(Integer id,ClientTypeVo clientTypeVo) throws SystemException {
        ClientType clientType1 = frontClientTypeMapper.selectByPrimaryKey(id);
        if(clientType1==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的客户类型不存在");
        }
        ClientType clientType = new ClientType();
        BeanUtils.copyProperties(clientTypeVo,clientType);
        clientType.setClientTypeId(id);
        frontClientTypeMapper.updateByPrimaryKeySelective(clientType);
    }
}
